<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary"  v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="details" highlight-current-row v-loading="listLoading" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="160">
			</el-table-column>
			<el-table-column prop="name" label="明细名" width="220" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
						   :page-size="pageSize"
						   :current-page="currentPage"
						   :total="total"
						   style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增和编辑界面-->
		<el-dialog :title='title' :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="明细名" prop="name">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="明细类型">
					<el-select v-model="saveForm.types_id" clearable value-key="id" placeholder="请选择">
						<el-option
								v-for="item in types"
								:key="item.id"
								:label="item.name"
								:value="item.id">
							<span style="float: left">{{ item.name }}</span>
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">保存</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, editDepartment, addDepartment } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
                    keyword: ''
				},
                details:[],
                types:[],
				total: 0,
                currentPage:1,
				pageSize:6,
                title:'',
				listLoading: false,
				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入明细名', trigger: 'blur' }
					]
				},
				saveFormVisible: false,//新增界面是否显示
				saveLoading: false,
				saveFormRules: {
					name: [
						{ required: true, message: '请输入明细名', trigger: 'blur' }
					]
				},
				//新增界面数据
				saveForm: {
                    id:null,
                    name: "",
					types_id:null
				}

			}
		},
		methods: {
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDetails();
			},
			search(){
			    this.currentPage=1;
			    this.getDetails();
			},
            getTypes(){
				this.$http.get("/dic/list")
					.then(result=>{
					    this.types=result.data
					})
					.catch(result=>{
                        this.$message({
                            type: 'erorr',
                            message: '程序员看小姐姐去了!'
                        });
					})
			},
			//获取明细列表
			getDetails() {
				let para = {
					currentPage: this.currentPage,
					pageSize:this.pageSize,
                    keyword: this.filters.keyword
				};
				this.listLoading = true;
				//NProgress.start();
				//发送axios请求
				//alert(this.$http)
				this.$http.post("/detail/page",para)
					.then(result=>{
                        this.listLoading = false;//开启忙等框
					    result=result.data;
					    console.log(result)
					    this.details=result.rows;
					    this.total=result.total;
					})
					.catch(result=>{
                        this.$message({
                            type: 'erorr',
                            message: '程序员看小姐姐去了!'
                        });
					})
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/detail/"+row.id)
						.then(result=>{
                            this.listLoading = false;
                            result=result.data;
						    if(result.success){
                                this.$message({
                                    message: result.message,
                                    type: 'success'
                                });
                                this.currentPage=1;
                                this.getDetails();
							}
						    else{
                                this.$message({
                                    message: result.message,
                                    type: 'error'
                                });
							}
						})
						.catch(result=>{
                            this.$message({
                                message: '系统繁忙,请稍后再试',
                                type: 'error'
                            });
						})
				}).catch(() => {
                    this.$message({
                        type: 'error',
                        message: '程序员看小姐姐去了!'
                    });
				});
			},
            //显示编辑界面
            handleEdit: function (index, row) {
                this.getTypes();//点击按钮的时候触发加载明细类型
                this.title = "编辑";
                this.saveFormVisible = true;
                this.saveForm = Object.assign({}, row);//克隆一份数据用来做回显
				console.log(row)
                this.$refs['saveForm'].resetFields();//重置表单校验
            },
			//显示新增界面
			handleAdd: function () {
                this.getTypes();//点击按钮的时候触发加载明细类型
                this.title = "新增";
				this.saveFormVisible = true;
				this.saveForm = {
                    id:null,
                    name: "",
                    types_id: null
				};
                this.$refs['saveForm'].resetFields();//重置表单校验
			},
			//新增和修改
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.saveLoading = true;
							let para = Object.assign({}, this.saveForm);
							this.$http.post("/detail/save",para)
								.then(result=>{
								    if(result.data.success){
                                        this.saveLoading = false;
                                        this.$message({
                                            message: result.data.message,
                                            type: 'success'
                                        });
                                        this.currentPage = 1;
                                        this.getDetails();
                                        this.$refs['saveForm'].resetFields();//重置表单校验
                                        this.saveFormVisible = false;//关闭模态框
									}else {
                                        this.$message({
                                            type: 'error',
                                            message: result.data.message
                                        });
                                        this.currentPage = 1;
                                        this.getDetails();
									}
								})
								.catch(result=>{
                                    this.saveLoading = false;//关闭按钮忙等
                                    this.$message({
                                        type: 'error',
                                        message: '程序员跑啦.系统出错啦'
                                    });
								})
						});
					}
				});
			},
		},
		mounted() {
			this.getDetails();
		}
	}

</script>

<style scoped>

</style>